﻿Public Class frmEditDepartment
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Primary_Code As String
    Dim Old_Name As String

#Region "Function"
    Function Validate_Data() As Boolean
        Try
            Validate_Data = False
            If Me.TxtDep_Name.Text = "" Then
                MsgBox("กรุณากรอกข้อมูลให้ครบ", MsgBoxStyle.Critical, "คำเตือน")
                Strsql = "select count(*) as cc from departments where dep_name = '" & Me.TxtDep_Name.Text & "'"
                'Dim Exist_Depname As Integer
                ' If _mysql.MySQLExecuteScalar(Strsql) = 0 Then Exit Function

            End If
            Validate_Data = True
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Function
    Sub Load_Autocomplete_Department()
        Try
            Strsql = "Select dep_name from departments"
            Dim DT_Department As New DataTable
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "departments")
            If DT_Department.Rows.Count = 0 Then Exit Sub
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_Department.Rows.Count - 1
                ColItemName.Add(DT_Department.Rows(i).Item("dep_name"))
            Next

            With TxtDep_Name
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .AutoCompleteCustomSource = ColItemName

            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region
    Private Sub frmEditDepartment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Autocomplete_Department()
        Try
            Strsql = "select ifnull(dep_name,'') as dep_name from departments where dep_id = " & Primary_Code
            'Dim SqlResult As Integer
            'SqlResult = _mysql.MySQLExecuteScalar(Strsql)
            'Select Case SqlResult
            '    Case -1
            '        With frmDebug
            '            .lblFormName.Text = Me.Name
            '            .lblFunctionName.Text = "Validate_Data"
            '            .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
            '            .MemoSQL.Text = Strsql
            '            .ShowDialog()
            '        End With
            '    Case Else

            Me.TxtDep_Name.Text = _mysql.MySQLExecuteScalar(Strsql)
            Old_Name = Me.TxtDep_Name.Text
            'End Select

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Validate_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            If MsgBox("คุณต้องการบันทึกข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

            If Me.TxtDep_Name.Text = Old_Name Then
                DialogResult = Windows.Forms.DialogResult.Cancel
                Exit Sub
                Me.Dispose()
            End If
            If Validate_Data() = False Then Exit Sub

            Strsql = "update departments set dep_name = '" & Me.TxtDep_Name.Text & "' where dep_id = " & Primary_Code
            If _mysql.MySQLExecute(Strsql) <> -1 Then
                MsgBox("ปรับปรุงเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
                DialogResult = Windows.Forms.DialogResult.OK


                ''Add Log
                'Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (15,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Primary_Code & "') "
                'Select Case (_mysql.MySQLExecute(Strsql))
                '    Case -1
                '        With frmDebug
                '            .lblFormName.Text = Me.Name
                '            .lblFunctionName.Text = "BtnOK_Click"
                '            .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                '            .MemoSQL.Text = Strsql
                '            .ShowDialog()

                '        End With
                'End Select

                Me.Dispose()
            Else
                MsgBox("ไม่สามารถปรับปรุงข้อมูลได้", MsgBoxStyle.Critical, "ผลการปรับปรุงข้อมูล")
            End If


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Dispose()
    End Sub
End Class